fix(mulesoft/exporter): status vars, perf, style, format#173
fix(mulesoft/exporter): status vars, perf, style, format#173
Conversation
| subscription = system.subscribe(:Bookings_1, :bookings) do |_subscription, mulesoft_bookings| | ||
| logger.debug { "DETECTED changed in Mulesoft Bookings.." } | ||
| logger.debug { "DETECTED change in Mulesoft Bookings..." } | ||
| @bookings = Array(Hash(String, Int64 | String | Nil)).from_json(mulesoft_bookings) |
There was a problem hiding this comment.
Doing something along these lines instead of the Hash would be much easier to maintain, faster, and ensures correctness
record MulesoftBooking,
getter title : String?,
getter recurring_master_id : Int64,
getter event_start : Int64,
getter event_end : Int64,
getter body : String do
include JSON::Serializable
@[JSON::Field(ignore: true)]
# Not sure about the efficiency of doing this pattern in a struct.
getter resolved_title : String do
"#{recurring_master_id} #{title.presence || body}"
end
def matches?(booking)
booking = booking.as_h if booking.is_a?(JSON::Any)
event_start == booking["event_start"]? && event_end == booking["event_end"]? && resolved_title == booking["title"]?
end
def to_place_calendar(
timezone : String,
system_email : String,
system_name : String,
)
{
title: resolved_title,
event_start: event_start,
event_end: event_end,
timezone: timezone,
description: body,
user_id: system_email,
location: system_name,
attendees: [{email: system_email, name: system_name}],
}
end
endThere was a problem hiding this comment.
Thanks so much, I'll grok this after some other tasks, then see if anything else should change and then test.
Yeah I was originally intending the booking hash and calendar event to be the same object but couldn't get that to work just due to my crystal inexperience. Ran out of time due to a delivery deadline so just implemented it this way.
Now that I have time it'll be great (and very educational for me) to implement it properly.
| subscription = system.subscribe(:Bookings_1, :bookings) do |_subscription, mulesoft_bookings| | ||
| logger.debug { "DETECTED changed in Mulesoft Bookings.." } | ||
| logger.debug { "DETECTED change in Mulesoft Bookings..." } | ||
| @bookings = Array(Hash(String, Int64 | String | Nil)).from_json(mulesoft_bookings) |
There was a problem hiding this comment.
| @bookings = Array(Hash(String, Int64 | String | Nil)).from_json(mulesoft_bookings) | |
| @bookings = Array(MulesoftBooking).from_json(mulesoft_bookings) |
| location: system.name.not_nil!, | ||
| } | ||
| logger.debug { ">>> EXPORTING booking #{new_event}" } | ||
| logger.debug { ">>> EXPORTING booking as: #{new_event}" } |
There was a problem hiding this comment.
protected def export_booking(booking : MulesoftBooking)
unless event_already_exists?(booking, @existing_events)
new_event = booking.to_place_calendar(@time_zone_string, system.email, system.name)
logger.debug { ">>> EXPORTING booking as: #{new_event}" }
calendar.create(**new_event)
end
end| protected def event_already_exists?(booking : Hash(String, Int64 | String | Nil), existing_events : Array(JSON::Any)) | ||
| existing_events.any? { |existing_event| booking_matches_event?(booking, existing_event.as_h) } | ||
| end | ||
|
|
||
| protected def events_match?(event_a : Hash(String, Int64 | String | Nil), event_b : Hash(String, JSON::Any)) | ||
| event_a.select("event_start", "event_end", "title") == event_b.select("event_start", "event_end", "title") | ||
| protected def booking_matches_event?(booking : Hash(String, Int64 | String | Nil), event : Hash(String, JSON::Any)) | ||
| booking.select("event_start", "event_end", "title") == event.select("event_start", "event_end", "title") | ||
| end |
There was a problem hiding this comment.
protected def event_already_exists?(booking : MulesoftBooking, existing_events : Array(JSON::Any))
logger.debug { "Checking for existing events that match: #{booking.resolved_title}" }
existing_events.any? { |event| booking.matches?(event) }
end|
@w-le I can whack in the suggestions I made if that's good for you? |
Thanks for these recommendations! The changes here should address them. Do let me know if anything else.
Apologies for committing directly to main branch instead of a PR from feat>main. I was in the middle of a client deployment change window and realised that the UAT instance was running off my fork of drivers while the PROD instance was on placeos/drivers. Will make sure to have new drivers reviewed in a PR PRIOR to deployments in the future.